package clam.apps.adu.metier;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Commentaire
{
	private static final String	TAG_ID		= "id";
	private static final String	TAG_TEXTE	= "texte";
	private static final String	TAG_DATE	= "date";
	private static final String	TAG_LOGIN	= "login";
	private static final String	TAG_NBPLUS	= "nbplus";
	private static final String	TAG_NBMOINS	= "nbmoins";
	private static final String	TAG_STORY	= "story";
	private int					id;
	private String				commentaire;
	private Date				date;
	private String				login;
	private int					nbplus;
	private int					nbmoins;
	private Story				story;
	private String				texte;
	
	public Commentaire()
	{
		this.story = new Story();
		this.date = new Date(0);
		this.login = "Anonyme";
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Commentaire [id=" + this.id + ", commentaire="
				+ this.commentaire + ", date=" + this.date + ", login="
				+ this.login + ", nbplus=" + this.nbplus + ", nbmoins="
				+ this.nbmoins + ", story=" + this.story.toString()
				+ ", texte=" + this.texte + "]";
	}
	
	public static ArrayList<Commentaire> findAllForStory(int idStory)
	{
		ParserJsonJavaManager pjjm = new ParserJsonJavaManager();
		
		return pjjm.findCommentairesByStory(idStory);
	}
	
	public boolean votePlus()
	{
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				ParserJsonJavaManager.URL_VOTE_PLUS_COMMENTAIRE + this.getId()); // On
		// se
		// connecte
		
		HttpResponse response;
		try
		{
			response = httpclient.execute(httppost); // On effectue la requete
			return (EntityUtils.toString(response.getEntity()) == "OK") ? true
					: false;
		}
		catch (ClientProtocolException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public boolean voteMoins()
	{
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				ParserJsonJavaManager.URL_VOTE_MOINS_COMMENTAIRE + this.getId()); // On
		// se
		// connecte
		
		HttpResponse response;
		try
		{
			response = httpclient.execute(httppost); // On effectue la requete
			return (EntityUtils.toString(response.getEntity()) == "OK") ? true
					: false;
		}
		catch (ClientProtocolException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public int getId()
	{
		return this.id;
	}
	
	public void setId(Integer id)
	{
		this.id = id;
	}
	
	public String getCommentaire()
	{
		return this.commentaire;
	}
	
	public void setCommentaire(String commentaire)
	{
		this.commentaire = commentaire;
	}
	
	public Date getDate()
	{
		return this.date;
	}
	
	public void setDate(Date date)
	{
		this.date = date;
	}
	
	public String getLogin()
	{
		return this.login;
	}
	
	public void setLogin(String login)
	{
		this.login = (login == "") ? "Anonyme" : login;
	}
	
	public int getNbplus()
	{
		return this.nbplus;
	}
	
	public void setNbplus(Integer nbPlus)
	{
		this.nbplus = nbPlus;
	}
	
	public int getNbmoins()
	{
		return this.nbmoins;
	}
	
	public void setNbmoins(Integer nbMoins)
	{
		this.nbmoins = nbMoins;
	}
	
	public Story getStory()
	{
		return this.story;
	}
	
	public void setStory(Story story)
	{
		this.story = story;
	}
	
	/**
	 * @return the texte
	 */
	public String getTexte()
	{
		return this.texte;
	}
	
	/**
	 * @param texte
	 *            the texte to set
	 */
	public void setTexte(String texte)
	{
		this.texte = texte;
	}
	
	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id)
	{
		this.id = id;
	}
	
	/**
	 * @param nbplus
	 *            the nbplus to set
	 */
	public void setNbplus(int nbplus)
	{
		this.nbplus = nbplus;
	}
	
	/**
	 * @param nbmoins
	 *            the nbmoins to set
	 */
	public void setNbmoins(int nbmoins)
	{
		this.nbmoins = nbmoins;
	}
	
	/**
	 * @return the tagId
	 */
	public static String getTagId()
	{
		return Commentaire.TAG_ID;
	}
	
	/**
	 * @return the tagTexte
	 */
	public static String getTagTexte()
	{
		return Commentaire.TAG_TEXTE;
	}
	
	/**
	 * @return the tagDate
	 */
	public static String getTagDate()
	{
		return Commentaire.TAG_DATE;
	}
	
	/**
	 * @return the tagLogin
	 */
	public static String getTagLogin()
	{
		return Commentaire.TAG_LOGIN;
	}
	
	/**
	 * @return the tagNbplus
	 */
	public static String getTagNbplus()
	{
		return Commentaire.TAG_NBPLUS;
	}
	
	/**
	 * @return the tagNbmoins
	 */
	public static String getTagNbmoins()
	{
		return Commentaire.TAG_NBMOINS;
	}
	
	/**
	 * @return the tagStory
	 */
	public static String getTagStory()
	{
		return Commentaire.TAG_STORY;
	}
	
}
